package com.aaa.threadadvanced.demo04;


import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: 阿里巴巴规范中指出：
 *                          在创建线程池时不建议使用工具类
 *  使用原生的线程池类ThreadPoolExecutor。 因为它任务原生的比较灵活。根据业务需求传入相应的参数
 * @date 2022/11/2
 */
public class Test01 {
    /*int corePoolSize, 核心线程的个数
    int maximumPoolSize, 最大线程的个数
    long keepAliveTime, 线程对象空闲的时间
    TimeUnit unit, 空闲时间单位
    BlockingQueue<Runnable> workQueue：等待队列*/
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 2000, TimeUnit.SECONDS, new LinkedBlockingDeque(3));
        //往线程池提交任务
        //执行的任务的多少是看设置的最多线程的个数，如何任务个数超过了设定线程的个数，会出现线程个数和任务个数不匹配的错误
        //Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@7ea987ac rejected from java.util.concurrent.ThreadPoolExecutor@12a3a380[Running, pool size = 5, active threads = 0, queued tasks = 0, completed tasks = 8]
        //	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
        //	at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
        //	at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
        //	at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
        //	at com.aaa.threadadvanced.demo04.Test01.main(Test.java:34)
        for (int i = 0; i <30; i++) {
            final int a=i;
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"~~"+a);
                }
            });
        }
    }

}
